const Database=require("better-sqlite3");
const path=require("path");
const db=new Database(path.join(__dirname,'./mydata.sqlite'),{
    fileMustExist:true,
    verbose:console.log
});

function findClassList(){
    let sql='select id,name from t_class';
    let stmt=db.prepare(sql);
    let rows=stmt.all();
    return rows;
}
function registerUser(user={}){
    let sql='insert into t_user (username,password) values (?,?)';
    let stmt=db.prepare(sql);
    let rows=stmt.run(user.username,user.password);
    return rows;
}

function findUser(user={}){
    let sql='select username,password from t_user where username=? and password=?';
    let stmt=db.prepare(sql);
    let rows=stmt.get(user.username,user.password);
    return rows;
}
function findUserList(){
    let sql='select id,username,password from t_user';
    let stmt=db.prepare(sql);
    let rows=stmt.all();
    return rows;
}
function findClassById(id=""){
    let sql='select id,name from t_class where id=?';
    let stmt=db.prepare(sql);
    let rows=stmt.get(id);
    return rows;
}
module.exports={
    findClassList,
    registerUser,
    findUser,
    findUserList,
    findClassById
}